package com.hq.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hq.sys.constat.SysConstant;
import com.hq.sys.eneity.Role;
import com.hq.sys.eneity.RoleUser;
import com.hq.sys.eneity.User;
import com.hq.sys.eneity.UserVo;
import com.hq.sys.mapper.RoleMapper;
import com.hq.sys.mapper.RoleUserMapper;
import com.hq.sys.mapper.UserMapper;
import com.hq.sys.service.UserService;
import com.hq.sys.util.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.weekend.Weekend;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleUserMapper roleUserMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public User login(UserVo userVo) {
        //明⽂
        //⽣成密⽂
        List<User> select = userMapper.select(new User().setLoginname(userVo.getLoginname()).setPwd(DigestUtils.md5DigestAsHex(userVo.getPwd().getBytes())));
        return select.size() > 0 ? select.get(0) : null;
    }
    //添加用户
    @Override
    public void addUser(UserVo userVo) {
        userMapper.insert(userVo);
    }
    //修改用户
    @Override
    public int updateUser(UserVo userVo) {
        return userMapper.updateByPrimaryKey(userVo);
    }
    //删除用户
    @Override
    public int deleteUser(Integer userid) {
        return userMapper.deleteByPrimaryKey(userid);
    }
    //批量删除用户
    @Override
    public int deleteBatchUser(Integer[] ids) {
        Arrays.asList(ids).stream().forEach(id->userMapper.deleteByPrimaryKey(id));
        return 0;
    }
    //查询所有用户
    @Override
    public DataGridView queryAllUser(UserVo userVo) {
        Page<Object> page = new PageHelper().startPage(userVo.getPage(), userVo.getLimit());
        Weekend<User> weekend = Weekend.of(User.class);
        if (StrUtil.isNotBlank(userVo.getRealname())){
            weekend.weekendCriteria().andLike(User::getRealname, "%" + userVo.getRealname() + "%");
        }
        if (StrUtil.isNotBlank(userVo.getLoginname())){
            weekend.weekendCriteria().andLike(User::getLoginname, "%" + userVo.getLoginname() + "%");
        }
        if (StrUtil.isNotBlank(userVo.getPosition())){
            weekend.weekendCriteria().andLike(User::getPosition, "%" + userVo.getPosition() + "%");
        }
        if (StrUtil.isNotBlank(userVo.getPhone())){
            weekend.weekendCriteria().andLike(User::getPhone, "%" + userVo.getPhone() + "%");
        }
        if (StrUtil.isNotBlank(userVo.getIdentity())){
            weekend.weekendCriteria().andLike(User::getIdentity, "%" + userVo.getIdentity() + "%");
        }
        if (Objects.nonNull(userVo.getSex())){
            weekend.weekendCriteria().andEqualTo(User::getSex, userVo.getSex());
        }
        List<User> data =userMapper.selectByExample(weekend);
        return new DataGridView(page.getTotal(), data);

    }
    //重置密码
    @Override
    public int resetUserPwd(UserVo userVo) {
        //根据id查询用户密码
        List<User> user = userMapper.select(new User().setUserid(userVo.getUserid()));
        return user.size() > 0 ? userMapper.updateByPrimaryKey(user.get(0).setPwd(DigestUtils.md5DigestAsHex(SysConstant.USER_DEFAULT_PWD.getBytes()))) : 0;
    }
    //根据ID查询用户
    @Override
    public User queryUserById(Integer id) {
        return null;
    }
    //保存用户和角色的关系
    @Override
    public void saveUserRole(UserVo userVo) {

    }
    //分配角色
    @Override
    public DataGridView initUserRole(Integer userid) {
        //根据userid查询用户拥有的角色id集合
        List<RoleUser> roleIds = roleUserMapper.select(new RoleUser().setUid(userid));
        List<Integer> ids = roleIds.stream().map(RoleUser::getRid).distinct().collect(Collectors.toList());
        //查询出userid对应的所有角色id集合
        List<Role> userRole = ids.stream()
                //.map(item -> roleMapper.selectByPrimaryKey(item))
                .map(roleMapper::selectByPrimaryKey)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        //所有可田项
        List<Role> allRole = roleMapper.select(new Role().setAvailable(SysConstant.AVAILABLE_TRUE));
        List<Map<String, Object>> data = new ArrayList<>();
        for (Role r1 : allRole) {
            Boolean LAY_CHECKED = false;
            for (Role r2 : userRole) {
                if (r1.getRoleid() == r2.getRoleid()) {
                    LAY_CHECKED = true;
                }
            }
            Map<String,Object> map=new HashMap<>();
            map.put("roleid",r1.getRoleid());
            map.put("rolename",r1.getRolename());
            map.put("rotedesd",r1.getRoledesc());
            map.put("LAY_CHECKED",LAY_CHECKED);
            data.add(map);
        }
        return new DataGridView(data);
    }
}
